<template>
  <div class="table">
      <div class="crumbs">
          <i class="el-icon-tickets"></i>收藏信息
      </div>
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="delAll">批量删除</el-button>
        <el-input v-model="select_word" size="mini" placeholder="筛选关键字" class="handle-input"></el-input>

      </div>
    </div>
    <el-table size="mini" border style="width:100%" height="680px" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" ></el-table-column>
      <el-table-column prop="name" label="歌手-歌名" align="center"></el-table-column>
      <el-table-column  label="操作" width="150" align="center" >
        <template slot-scope="scope">
        <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

     <el-dialog
      title="删除歌曲"
      :visible.sync="deleteVisible"
      width="300px"
      center
    >
     <div align="center">删除了就不能恢复了哦，确定是否删除？</div>
      <span slot="footer">
          <el-button size="mini" @click="deleteVisible=false">取消</el-button>
          <el-button size="mini" @click="deleteRow">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {mixin} from '../mixins/index';
import {querySongBySongId,queryCollectionByUserId,deleteCollection} from '../api/index';
export default {
  mixins: [mixin],
  props:['id'],
  data() {
    return {
      deleteVisible: false,//删除弹窗是否显示
      tableData:[],
      temData:[],
      select_word: '',
      idx: -1,  //当前选择项
      mutipleSelection: [], //选择的项
    }
  },
  watch:{
    // 搜索框里面的内容发生变化时，搜索的结果table列表里的内容跟着他的内容发生变化
    select_word: function(){
      if(this.select_word == ''){
        this.tableData = this.temData;
      }else{
        this.tableData=[];
        for(let item of this.temData){
          if(item.name.includes(this.select_word)){
            this.tableData.push(item);
          }
        }
      }
    }
  },
  created(){
      this.getData();
  },
  methods:{
    //查询用户所有收藏
    getData(){
        this.tableData=[];
        this.temData=[];
        queryCollectionByUserId(this.$route.query.id).then(res =>{
           for(let item of res){
             this.getSong(item.songId);
           }
        })
    },
    //根据歌曲id查询歌曲对象，放到tempdata和tabledata中
    getSong(id){
      querySongBySongId(id)
      .then(res =>{
        this.temData.push(res);
        this.tableData.push(res);
      })
      .catch(err =>{
        console.log(err);
      });
    },

      // 删除一个歌曲
      deleteRow(){
        deleteCollection(this.$route.query.id,this.idx)
        .then(res =>{
            if(res){
                this.getData();
                this.notify("删除成功","success");
            }else{
                this.notify("删除失败","error");
            }
        })
        .catch(err =>{
            console.log(err);
        });
        this.deleteVisible = false;
      },
       // 批量删除已经选择的项
        delAll(){
            for(let item of this.mutipleSelection){
                this.handleDelete(item.id);
                this.deleteRow();
            }
            // this.deleteVisible = true;
            this.mutipleSelection = [];
        }

  }
};
</script>
<style scoped>
.handle-box {
  margin-bottom: 20px;
}
.song-img{
    width: 100%;
    height: 80px;
    border-radius: 5px;
    margin-bottom: 5px;
    overflow: hidden;
}
.handle-input{
  width: 300px;
  display: inline-block;
}
.pagination{
  display: flex;
  justify-content: center;
}
.play{
  position:absolute;
  z-index: 100;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  top: 16px;
  left: 16px;
}
.icon{
  width: 2em;
  height: 2em;
  color: bisque;
  fill: currentColor;
  overflow: hidden;
}
</style>
